package com.ruoyi.system.service.impl;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.ProjectManageDetail;
import com.ruoyi.system.domain.vo.ProjectManageRemarkRespVO;
import com.ruoyi.system.domain.vo.RemarkVO;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ProjectManageRemarkMapper;
import com.ruoyi.system.domain.ProjectManageRemark;
import com.ruoyi.system.service.IProjectManageRemarkService;

import javax.annotation.Resource;

/**
 * 项目管理评论Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-01
 */
@Service
public class ProjectManageRemarkServiceImpl extends ServiceImpl<ProjectManageRemarkMapper, ProjectManageRemark> implements IProjectManageRemarkService {
    @Resource
    private ProjectManageRemarkMapper projectManageRemarkMapper;

    @Resource
    private ISysUserService iSysUserService;

    /**
     * 查询项目管理评论
     *
     * @param id 项目管理评论主键
     * @return 项目管理评论
     */
    @Override
    public ProjectManageRemark selectProjectManageRemarkById(Long id) {
        return projectManageRemarkMapper.selectProjectManageRemarkById(id);
    }

    /**
     * 查询项目管理评论列表
     *
     * @param projectManageRemark 项目管理评论
     * @return 项目管理评论
     */
    @Override
    public List<ProjectManageRemark> selectProjectManageRemarkList(ProjectManageRemark projectManageRemark) {
        return projectManageRemarkMapper.selectProjectManageRemarkList(projectManageRemark);
    }

    /**
     * 新增项目管理评论
     *
     * @param projectManageRemark 项目管理评论
     * @return 结果
     */
    @Override
    public int insertProjectManageRemark(ProjectManageRemark projectManageRemark) {
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = iSysUserService.selectUserById(userId);
        if (sysUser == null) {
            throw new ServiceException("插入评论失败，用户不存在！");
        }
        projectManageRemark.setCreateTime(DateUtils.getNowDate());
        projectManageRemark.setUserId(userId);
        projectManageRemark.setUserName(sysUser.getNickName());

        save(projectManageRemark);
        return 1;
    }

    /**
     * 修改项目管理评论
     *
     * @param projectManageRemark 项目管理评论
     * @return 结果
     */
    @Override
    public int updateProjectManageRemark(ProjectManageRemark projectManageRemark) {
        projectManageRemark.setUpdateTime(DateUtils.getNowDate());
        return projectManageRemarkMapper.updateProjectManageRemark(projectManageRemark);
    }

    /**
     * 批量删除项目管理评论
     *
     * @param ids 需要删除的项目管理评论主键
     * @return 结果
     */
    @Override
    public int deleteProjectManageRemarkByIds(Long[] ids) {
        return projectManageRemarkMapper.deleteProjectManageRemarkByIds(ids);
    }

    /**
     * 删除项目管理评论信息
     *
     * @param id 项目管理评论主键
     * @return 结果
     */
    @Override
    public int deleteProjectManageRemarkById(Long id) {
        return projectManageRemarkMapper.deleteProjectManageRemarkById(id);
    }

    /**
     * @return
     */
    @Override
    public List<ProjectManageRemarkRespVO> getList(ProjectManageRemark projectManageRemark) {
        LambdaQueryWrapper<ProjectManageRemark> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ProjectManageRemark::getProjectManageDetailId, projectManageRemark.getProjectManageDetailId());
        lqw.orderByDesc(ProjectManageRemark::getId);
        List<ProjectManageRemark> selectList = projectManageRemarkMapper.selectList(lqw);
        if (CollUtil.isEmpty(selectList)) {
            return CollUtil.newArrayList();
        }
        // 处理二级
        List<ProjectManageRemark> oneList = selectList.stream().filter(item -> item.getPid() == 0).collect(Collectors.toList());
        return oneList.stream().map(item -> {
            ProjectManageRemarkRespVO respVO = new ProjectManageRemarkRespVO();
            respVO.setId(item.getId());
            respVO.setComment(item.getContent());
            respVO.setName(item.getUserName());
            respVO.setTime(item.getCreateTime());
            respVO.setCommentNum(0);
            respVO.setLike(0);

            List<ProjectManageRemark> reply = selectList.stream()
                    .filter(subItem -> subItem.getPid().equals(item.getId()))
                    .collect(Collectors.toList());
            if (CollUtil.isNotEmpty(reply)) {
                respVO.setReply(reply.stream().map(subItem -> {
                    RemarkVO remarkVO = new RemarkVO();
                    remarkVO.setComment(subItem.getContent());
                    remarkVO.setTime(subItem.getCreateTime());
                    remarkVO.setFrom(subItem.getUserName());
                    remarkVO.setFromId(subItem.getId());

                    //父级
                    remarkVO.setTo(item.getUserName());
                    remarkVO.setToId(item.getId());
                    return remarkVO;
                }).collect(Collectors.toList()));
            }

            if (CollUtil.isNotEmpty(respVO.getReply())) {
                respVO.setCommentNum(respVO.getReply().size());
            }

            return respVO;
        }).collect(Collectors.toList());
    }
}
